package org.esprit.ocm.client.model.ec2.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.ec2.InstanceRecord;
import org.esprit.ocm.dto.ec2.impl.InstanceDescriptionDto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class InstanceLocalDS extends DataSource {

	private static InstanceLocalDS	instance	= null;

	public static InstanceLocalDS getInstance(List<InstanceDescriptionDto> instances) {
		// if (instance == null) {
		instance = new InstanceLocalDS("instanceLocalDS", instances);
		// }
		return instance;
	}

	public InstanceLocalDS(String id, List<InstanceDescriptionDto> instances) {

		// setID(id);

		DataSourceTextField resId = new DataSourceTextField("resId", "resId");
		DataSourceTextField owner = new DataSourceTextField("owner", "owner");
		DataSourceTextField imageId = new DataSourceTextField("imageId", "imageId");
		DataSourceTextField instanceId = new DataSourceTextField("instanceId", "instanceId");
		DataSourceTextField privateDnsName = new DataSourceTextField("privateDnsName", "privateDnsName");
		DataSourceTextField dnsName = new DataSourceTextField("dnsName", "dnsName");
		DataSourceTextField reason = new DataSourceTextField("reason", "reason");
		DataSourceTextField keyName = new DataSourceTextField("keyName", "keyName");
		DataSourceTextField launchTime = new DataSourceTextField("launchTime", "launchTime");
		DataSourceTextField launchIndex = new DataSourceTextField("launchIndex", "launchIndex");
		DataSourceTextField availabilityZone = new DataSourceTextField("availabilityZone", "availabilityZone");
		DataSourceTextField kernelId = new DataSourceTextField("kernelId", "kernelId");
		DataSourceTextField ramdiskId = new DataSourceTextField("ramdiskId", "ramdiskId");
		DataSourceTextField platform = new DataSourceTextField("platform", "platform");
		DataSourceTextField state = new DataSourceTextField("state", "state");
		DataSourceTextField monitoring = new DataSourceTextField("monitoring", "monitoring");
		DataSourceTextField groups = new DataSourceTextField("groups", "groups");
		DataSourceTextField instanceType = new DataSourceTextField("instanceType", "instanceType");

		setFields(resId, owner, imageId, instanceId, privateDnsName, dnsName, groups, keyName, instanceType, launchTime, launchIndex,
				availabilityZone, kernelId, ramdiskId, platform, state, monitoring);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (InstanceDescriptionDto from : instances) {

			System.out.println("***" + from.getImageId());
			InstanceRecord to = new InstanceRecord();

			to.setResId(from.getResId());
			to.setOwner(from.getOwner());

			to.setImageId(from.getImageId());
			to.setInstanceId(from.getInstanceId());
			to.setPrivateDnsName(from.getPrivateDnsName());
			to.setDnsName(from.getDnsName());
			to.setReason(from.getReason());
			to.setGroups(from.getGroups());
			to.setKeyName(from.getKeyName());
			to.setInstanceType(from.getInstanceType());
			to.setLaunchTime(from.getLaunchTime());
			to.setLaunchIndex(from.getLaunchIndex());
			to.setAvailabilityZone(from.getAvailabilityZone());
			to.setKernelId(from.getKernelId());
			to.setRamdiskId(from.getRamdiskId());
			to.setPlatform(from.getPlatform());
			to.setState(from.getState());
			to.setMonitoring(from.isMonitoring());

			l.add(to);
		}

		setTestData(l.toArray(new InstanceRecord[0]));
		setClientOnly(true);
	}

}
